package com.atguigu.juc.test01;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.zip.ZipEntry;

public class ShuZiOrZiMu {
    public static void main(String[] args) {
        SZ sz = new SZ();

        //线程操作 资源类
        new Thread(()->{
        sz.outShuZi();
        },"a").start();
        new Thread(()->{
            sz.outziMu();
        },"b").start();
    }
}

//资源类
class SZ {
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();
    private Condition condition2= lock.newCondition();


    private  int shuzhi = 1;
    private  char zimu = 'A';
    private   int i = 0;//标识

    public void outShuZi() {

        lock.lock();  // block until condition holds

        try {
            while (shuzhi <= 52) {//循环52次

                while (i == 2) {//每输出两次等待一次

                    condition1.await();
                }
                System.out.println(Thread.currentThread().getName() + "________>" + shuzhi++ +"  "+i);
                i++;//增加标识
                    condition2.signal();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void outziMu() {
        lock.lock();
        try {
            while (zimu <= 'Z') {

                while (i != 2) {//如果不等于2 沉睡
                    condition2.await();
                }
                System.out.println(Thread.currentThread().getName() + "  :" + zimu++ + "  "+i);
                i=0;
                condition1.signal();

            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

}
